#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tpf_js_func.py 
@version:
@time: 2022/06/21 
"""
from TapClientCore.T05_TM_feature.v2.TMF11_js_func.tmf_js_func import TMFJsFunc
from TapClientCore.T01_init_conf.path_conf import PathConf


class TPFJsFunc(TMFJsFunc):
    # 创建自定义函数
    """
        {
          "type": "custom",
          "format": "RobotJS(record)",
          "parameters_desc": "",
          "return_value": "",
          "script": "function RobotJS(record) {\n  return record\n}",
          "id": "62b13773ddcd4a634371386d",
          "function_name": "RobotJS",
          "parameters": "record",
          "function_body": "{\n  return record\n}",
        }

    [
      {
        "type": "jar",
        "fileId": "62b13843ddcd4a6343713952",
        "fileName": "demo-plugin-jar-1.0-SNAPSHOT.jar",
        "packageName": "com.tapdata.demo.plugin",
        "function_name": "JARtoString",
        "className": "com.tapdata.demo.plugin.Demo",
        "methodName": "toString",
        "describe": "",
        "format": "JARtoString(arg0:String)",
        "parameters_desc": "",
        "return_value": "",
      }
    ]

    """

    def tpf_create_custom_func(self, custom_data):
        self.tmf_delete_js_func_by_name(custom_data.get('function_name'))
        response = self.tmf_create_func(custom_data)
        new_data = response.get('data')
        new_data.update({'parameters_desc': '参数说明', 'describe': '输入啥返回啥'})
        return self.tmf_patch_func_by_name(new_data.get('function_name'), new_data)

        # 创建jar函数

    def tpf_create_jar_func(self, jar_data, path=PathConf.JAR_DEMO_PATH):
        for one in jar_data:
            self.tmf_delete_js_func_by_name(one.get('function_name'))
        jar_id = self.tm_post_file_upload(path).get('data').get('id')
        for one in jar_data:
            one.update({'fileId': jar_id})
        response = self.tmf_create_func(jar_data)
        new_data = response.get('data')
        for one in new_data:
            one.update({'parameters_desc': '参数说明', 'describe': 'jar 包'})
            self.tmf_patch_func_by_name(one.get('function_name'), one)


if __name__ == '__main__':
    T = TPFJsFunc()
    from TapClientCore.T01_init_conf.path_conf import PathConf

    res = T.tpf_create_jar_func(
        PathConf.JAR_DEMO_PATH,
        [
            {
                "type": "jar",
                "fileName": "demo-plugin-jar-1.0-SNAPSHOT.jar",
                "packageName": "com.tapdata.demo.plugin",
                "function_name": "JARtoString",
                "className": "com.tapdata.demo.plugin.Demo",
                "methodName": "toString",
                "format": "JARtoString(arg0:String)",
            }
        ]
    )
